<?php
/**
 * Created by PhpStorm.
 * User: Hello pan
 * Date: 9/13/25
 * Time: 9:32 AM
 */

namespace app\controllers;

use app\models\Country;
use yii\data\Pagination;
use yii\helpers\VarDumper;
use yii\web\Controller;

class Country1Controller extends Controller
{


    public function actionCreate()
    {
        $c = new Country();


    }

    public function actionIndex()
    {
        // 返回查询对象
        $query = Country::find();
        $pages = new Pagination([
            'defaultPageSize' => 5,
            'totalCount' => $query->count()
        ]);

        $list = $query->orderBy('name')
            ->offset($pages->offset)
            ->limit($pages->limit)
            ->all();

        return $this->render('index', [
            'countrys' => $list,
            'pagination' => $pages,
        ]);

    }


    public function actionTest()
    {
        $model = new Country();
        $list = $model::find()->all();

//        VarDumper::dump(json_encode($list));

        // 获取一条数据，返回对应的对象
        $res = $model::findOne('us');
        var_dump($res->code);
        var_dump($res->attributes);

        // 修改 name 为 “U.S.A.” 并在数据库中保存更改
        $res->name = 'U.S.A.';
        $res->save();

    }
}